/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

categoriesOpened = new Array();
tabActive = new Array();
tabActive.push("#asikasik");
tabActive.push("riza_sp");
tabActive.push("aang");
tabActive.push("hot");

activatedTab = "riza_sp";

for (var i = 1; i < 4; ++i) {
    categoriesOpened.push(false);
    categoriesOpened[i] = false;
}

function toggleCategory(id) {
    if (!categoriesOpened[id]) {
        $("iconcategory"+id).setAttribute("src", "designs/category_opened.png");
        show("roomscategory"+id);
        categoriesOpened[id] = true;
    } else {
        $("iconcategory"+id).setAttribute("src", "designs/category.png");
        hide("roomscategory"+id);
        categoriesOpened[id] = false;
    }
}

function toggleRoomUserList() {
    if (!rulOpened) {
        show('rulpayload');
        rulOpened = true;
    } else {
        hide('rulpayload');
        rulOpened = false;
    }
}

function openTab(name) {
    var tabPos = searchTabActive(name);
    if (tabPos == -1) {
        tabPos = tabActive.length;
        tabActive.push(name);
        addTab(name);
        $('tabs').style.width = 174*(tabActive.length)+"px";
    } else {
        
    }
    activateTab(tabActive[tabPos]);
    handleEmptyChat();
} // apabila mengkilk pilihan di samping...

function addTab(name) {
    var newTab = document.createElement("div");
    newTab.setAttribute("class", "tabtitle tabopened");
    newTab.id="tab"+name;
    newTab.innerHTML = "<a href=\"javascript:activateTab('"+name+"');\" class=\"tabtitletext\">"+name+"</a>";
    newTab.innerHTML += "<a href=\"javascript:closeTab('"+name+"')\" class=\"tabcloser\">x</a>";
    $('tabs').appendChild(newTab);
} // menambahkan tab di daftar tab

function closeTab(name) {
    var tabPos = searchTabActive(name);
    var tempTabActive = new Array();
    for (var i = 0; i < tabActive.length; ++i) {
        if (i != tabPos) {
            tempTabActive.push(tabActive[i]);
        }
    }
    tabActive = tempTabActive;
    if (tabPos >= tabActive.length)
        tabPos = tabActive.length - 1;
    removeTab(name);
    $('tabs').style.width = 174*(tabActive.length)+"px";
    if (tabPos != -1) {
        if (activatedTab == name) {
            activateTab(tabActive[tabPos]);
        }
    }
    handleEmptyChat();
    // close logically
} // menutup tab dengan tombol close

function removeTab(name) { // mengrangi tab dari daftar tab
    $('tabs').removeChild($('tab'+name));
}

function handleEmptyChat() {
    if (tabActive.length == 0) {
        $('chatinput').setAttribute("disabled", "disabled");
        $('playground').innerHTML = "<div class=\"notif\">Start chatting now!<br />Choose Rooms or Friends to chat on your left.</div>"
    } else {
        $('chatinput').removeAttribute("disabled");
        $('chatinput').focus();
        $('playground').innerHTML = "";
    }
}

function activateTab(name) {
    if (activatedTab != name) {
        if ($("tab"+activatedTab) != null && activatedTab != name) {
            disactivateTab(activatedTab);
        }
        $('tab'+name).className = "tabtitle tabopened";
        activatedTab = name;
        $('playground').innerHTML = ""; // kosongkan atau ganti dengan yang terbaru...
        var nameString = new String(name);
        if (nameString.charAt(0) == "#") {
            show('rultitle');
            // update dengan isi rul sekarang
        } else {
            hide('rulpayload');
            hide('rultitle');
        }
    }
}

function disactivateTab(name) {
    $('tab'+name).className = "tabtitle tabclosed";
}

function searchTabActive(name) {
    for (var i = 0; i < tabActive.length; ++i) {
        if (tabActive[i] == name)
            return i;
    }
    return -1;
}
